package com.jtech.together.mvp.presenter;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.jtech.together.manager.UserManager;
import com.jtech.together.model.UserModel;
import com.jtech.together.mvp.contract.MyProfileContract;
import com.jtech.together.util.BmobUtil;

import java.io.File;

import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.UploadFileListener;

/**
 * 我的信息
 * Created by wuxubaiyang on 2017/5/10.
 */
public class MyProfilePresenter implements MyProfileContract.Presenter {
    private Context context;
    private MyProfileContract.View view;

    public MyProfilePresenter(Context context, MyProfileContract.View view, Bundle bundle) {
        this.context = context;
        this.view = view;
    }

    @Override
    public UserModel getMyUserProfile() {
        return UserManager.getCurrentUser();
    }

    @Override
    public void updateAvatar(final String avatarPath) {
        //先上传用户头像再更新信息
        final BmobFile bmobFile = new BmobFile(new File(avatarPath));
        bmobFile.uploadblock(new UploadFileListener() {
            @Override
            public void done(BmobException e) {
                if (null == e) {
                    final String avatarUrl = bmobFile.getFileUrl();
                    final UserModel userModel = UserManager.getCurrentUser();
                    userModel.setAvatar(avatarUrl);
                    userModel.update(new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if (null == e) {
                                view.updateAvatarSuccess(avatarPath, avatarUrl);
                                //更新本地即时通讯的用户表
                                UserManager.updateIMUserInfo(userModel);
                            } else {
                                view.updateFail(BmobUtil.getErrorMessage(e.getErrorCode()));
                            }
                        }
                    });
                } else {
                    view.updateFail("头像上传失败，请重试");
                }
            }

            @Override
            public void onProgress(Integer value) {
                Log.i("avatarUploadProgress：", value + "");
            }
        });
    }

    @Override
    public void updateSex(final int sex) {
        final UserModel userModel = UserManager.getCurrentUser();
        userModel.setSex(sex);
        userModel.update(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (null == e) {
                    view.updateSexSuccess(sex);
                    //更新本地即时通讯的用户表
                    UserManager.updateIMUserInfo(userModel);
                } else {
                    view.updateFail(BmobUtil.getErrorMessage(e.getErrorCode()));
                }
            }
        });
    }

    @Override
    public void updateBirthday(final long birthday) {
        final UserModel userModel = UserManager.getCurrentUser();
        userModel.setBirthday(birthday);
        userModel.update(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (null == e) {
                    view.updateBirthdaySuccess(birthday);
                    //更新本地即时通讯的用户表
                    UserManager.updateIMUserInfo(userModel);
                } else {
                    view.updateFail(BmobUtil.getErrorMessage(e.getErrorCode()));
                }
            }
        });
    }
}